package service

import (
	"errors"
	"os"
	"path/filepath"

	"sieve_admin_server_userpass/pkg/app"
	"sieve_admin_server_userpass/pkg/config"
	"sieve_admin_server_userpass/pkg/im3/event/cli"
	"sieve_admin_server_userpass/pkg/log"
	"sieve_admin_server_userpass/pkg/mp/child"
	"sieve_admin_server_userpass/pkg/util/rand"
)

var (
	fHost    = config.String("server.host", "", "main process host")
	fPort    = config.Int("server.port", 0, "main process port")
	fCdnHost = config.String("cdn.host", "https://img.feihutv.cn/", "cdn host")
)

func init() {
	app.OnStart(func() {
		if err := child.HandleMessage(handleApi); err != nil {
			log.Fatal(err)
		}
	})
}

func ServerHost() string {
	return *fHost
}

func ServerPort() int {
	return *fPort
}

func CDNHost() string {
	return *fCdnHost
}

//

func init() {
	cli.Name = filepath.Base(os.Args[0])
	cli.Pwd = rand.RandStr(32)
}

func init() { AddApiHandler(&api_GetEventPwd{}) }

type api_GetEventPwd struct{}

func (a *api_GetEventPwd) Handle(caller string) interface{} {
	if caller != "event" {
		return errors.New("svc-event only")
	}
	return cli.Pwd
}
